﻿'Description:角色类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Wangjh
'create Date:2019-1-07
'Rewriter:
'Rewrite Date:
Imports DatabaseAccess.DBUtility
Imports BusinessRule.IBusiness
Imports System.Text

Public Class uLMSRoleBusiness
    Implements uLMSRoleIBusiness
    ''' <summary>
    ''' 查询角色信息
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetRoleInfo(ByVal para As Dictionary(Of String, String)) As DataTable Implements IBusiness.uLMSRoleIBusiness.GetRoleInfo
        Dim strSql As String = <string>SELECT *  FROM roledef rd where 1=1 </string>
        If para.ContainsKey("RoleName") Then
            strSql += String.Format(" and rd.rolename='{0}'", para("RoleName"))
        End If
        If para.ContainsKey("FactoryID") Then
            strSql += String.Format(" and rd.FactoryID='{0}'", para("FactoryID"))
        End If
        Return DatabaseHelper.Query_ReturnDatTable(strSql)
    End Function

    ''' <summary>
    ''' 获取员工列表
    ''' </summary>
    Public Function GetEmployee(ByVal para As Dictionary(Of String, String)) As System.Data.DataTable Implements IBusiness.uLMSRoleIBusiness.GetEmployee
        Dim strSQL As New StringBuilder
        strSQL.AppendLine("SELECT sv.factoryid,sv.workcenterid,em.* FROM employee em")
        strSQL.AppendLine(" LEFT JOIN Sessionvalues sv ON em.employeeid=sv.employeeid")
        strSQL.AppendFormat("WHERE sv.factoryid='{0}'", para("FactoryID"))
        If para.ContainsKey("WorkcenterID") Then
            strSQL.AppendFormat("AND sv.workcenterid='{0}'", para("WorkcenterID"))
        End If
        If para.ContainsKey("FullName") Then
            strSQL.AppendFormat("AND em.FullName='{0}'", para("FullName"))
        End If
        Dim dt As DataTable = DatabaseHelper.Query_ReturnDatTable(strSQL.ToString)
        If dt Is Nothing Then
            GetEmployee = New DataTable
        Else
            GetEmployee = dt
        End If
    End Function

    ''' <summary>
    ''' 保存角色信息
    ''' </summary>
    ''' <param name="roleDTO"></param>
    ''' <param name="employeeLists"></param>
    ''' <param name="type">1修改 2添加 3删除</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function SaveRoleInfo(ByVal roleDTO As DTO.RoleDTO, ByVal employeeLists As List(Of String), ByVal type As Integer) As Boolean Implements IBusiness.uLMSRoleIBusiness.SaveRoleInfo
        SaveRoleInfo = False
        Dim sqlType As String = ""
        Select Case type
            Case 1
                sqlType = "update"
            Case 2
                sqlType = "insert"
            Case 3
                sqlType = "delete"
        End Select

        If DatabaseHelper.ExcuteDTO(roleDTO, sqlType, "RoleID") = False Then
            Exit Function
        End If
   
        If type <> 2 Then
            '查询该角色下所有人员
            Dim emDt As DataTable = DatabaseHelper.Query_ReturnDatTable("select * from employeerole er WHERE er.roleid='" + roleDTO.RoleID + "'")
            For Each emRow As DataRow In emDt.Rows
                If Not employeeLists.Contains(emRow("employeeid").ToString) Then '该角色下需要删除的人员
                    '删除角色列表
                    Dim deleteSql As String = "DELETE employeerole er WHERE er.employeeroleid='" + emRow("employeeroleid").ToString + "'"
                    DatabaseHelper.ExecuteSql(deleteSql)
                End If
            Next
        End If
       
        If type <> 3 Then
            Dim strSql As String = <string>INSERT INTO employeerole er (er.cdotypeid,er.employeeid,er.employeeroleid,er.roleid)</string>
            Dim judgeSql As String = "SELECT *  FROM employeerole er WHERE er.roleid='" + roleDTO.RoleID + "'"
            For Each employeeID In employeeLists
                Dim judgeSql2 = " and er.employeeid='" + employeeID + "'"
                If DatabaseHelper.Query_ReturnDatTable(judgeSql + judgeSql2).Rows.Count > 0 Then '已经存在
                    Continue For
                End If
                Dim connectSql As String = String.Format(" VALUES(7782,'{0}','{1}','{2}')", employeeID, GenerateGuidTo16StringID(), roleDTO.RoleID)
                DatabaseHelper.ExecuteSql(strSql + connectSql)
            Next
        Else '删除角色所有人员列表
            Dim deleteSql As String = "DELETE employeerole er WHERE er.roleid='" + roleDTO.RoleID + "'"
            DatabaseHelper.ExecuteSql(deleteSql)
        End If

        SaveRoleInfo = True
    End Function

    ''' <summary>
    ''' 16位ID
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Function GenerateGuidTo16StringID() As String
        Dim buffer As Byte() = Guid.NewGuid().ToByteArray()
        Dim value As String = Hex(BitConverter.ToInt64(buffer, 0)).ToLower()
        While (value.Length < 16)
            value = "0" & value
        End While
        Return value
    End Function
End Class
